<?php
// +----------------------------------------------------------------------
// | 知识星球[基于ThinkPHP5开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 abcblockchain.io
// +----------------------------------------------------------------------
// | 后台管理使用了HisiPHP
// +----------------------------------------------------------------------
// | Author: abcblockchain.io
// +----------------------------------------------------------------------
namespace app\common\model;

use think\Model;
use think\Db;
use app\common\model\UsersCurrency as UsersCurrencyModel;
use app\common\model\Currency as currencyModel;

/**
 * Exchange 模型
 * @package app\common\model
 */
class Exchange extends Model
{
    // 定义时间戳字段名
    protected $createTime = 'ctime';
    protected $updateTime = false;

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    /**
     * 获取用户交易记录
     * @author
     * @param $userId
     * @param int $type
     * @param int $startNo
     * @param int $limit
     * @return array|false|\PDOStatement|string|\think\Collection
     */
    public function getUserTransList($userId, $currencyId, $type = 0, $startNo = 0, $limit = 10)
    {
        $map['ex.uid'] = $userId;
        $map['ex.currency_id'] = $currencyId;
        if ($type > 0) {
            if($type == 3){
                $map['ex.opt_type'] = array('in','6,7');
            }else{
                $map['ex.opt_type'] = $type;
            }
        }
        $startNo = $startNo * $limit;
        $transList = $this->alias('ex')->join('currency c', 'ex.currency_id = c.id')->join('group g', 'ex.group_id=g.id', 'LEFT')->field('ex.*,c.currency_mark,g.name')->where($map)->limit($startNo, $limit)->order('ctime desc')->select();
        return $transList ? $transList : array();
    }

    /**
     * 申请提现
     * @author
     * @param $userId
     * @param $currencyId
     * @param $toAddress
     * @param $num
     */
    public function withdrawCurrency($userId, $currencyId, $toAddress, $num)
    {
        $currencyModel = new currencyModel();
        $currencyInfo = $currencyModel->currencyInfo($currencyId);
        if ($currencyInfo['fee']) {
            $fee = ($num * $currencyInfo['fee']) / 100;
            $number = $num - $fee;
        } else {
            $fee = 0;
            $number = $num;
        }
        $insertData = [];
        $insertData['uid'] = $userId;
        $insertData['currency_id'] = $currencyId;
        $insertData['to'] = $toAddress;
        $insertData['number'] = $number;
        $insertData['opt_type'] = 2;
        $insertData['status'] = 1;
        $insertData['ctime'] = time();
        $insertData['fee'] = $fee;
        Db::startTrans();
        try {
            Db::name('exchange')->insert($insertData);
            $map['user_id'] = $userId;
            $map['currency_id'] = $currencyId;
            Db::name('users_currency')->where($map)->setDec('num', $num);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            return false;
        }

    }

}